part of '../index.dart';

class BuildMessageTime extends StatelessWidget {
  final int timestamp;

  const BuildMessageTime({super.key, required this.timestamp});

  bool get _isToday {
    final date = DateTime.fromMillisecondsSinceEpoch(timestamp);
    final now = DateTime.now();
    return date.year == now.year &&
        date.month == now.month &&
        date.day == now.day;
  }

  bool get _isYesterday {
    final date = DateTime.fromMillisecondsSinceEpoch(timestamp);
    final yesterday = DateTime.now().subtract(const Duration(days: 1));
    return date.year == yesterday.year &&
        date.month == yesterday.month &&
        date.day == yesterday.day;
  }

  String _formatTime() {
    final dateTime = DateTime.fromMillisecondsSinceEpoch(timestamp);
    if (_isToday) {
      return '${T.current.c_today} ${intl.DateFormat('HH:mm').format(dateTime)}';
    } else if (_isYesterday) {
      return '${T.current.c_yesterday} ${intl.DateFormat('HH:mm').format(dateTime)}';
    } else if (dateTime.year == DateTime.now().year) {
      return intl.DateFormat('MM/dd HH:mm').format(dateTime);
    } else {
      return intl.DateFormat('yyyy/MM/dd HH:mm').format(dateTime);
    }
  }

  @override
  Widget build(BuildContext context) {
    return SafeArea(
      top: false,
      bottom: false,
      child: Text(
        _formatTime(),
        textAlign: TextAlign.center,
        style: const TextStyle(fontSize: 12, color: Color(0xFFC1C1C1)),
      ),
    );
  }
}
